package com.ruoyi.quartz.service;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class MediaServerService {

    public static final String API_HOST = "http://192.168.1.110:5800";

    public static final String ACCESS_KEY = "047I4WS1-U51UBO6W-1J4BT21P-MF17IT99-92J8WIHU-944Q4KIW";

    public static final String START_RECORD= "/MediaServer/StartRecord";

    public static final String STOP_RECORD= "/MediaServer/StopRecord";

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 调用开始录制接口
     * @param mediaServerId 流媒体服务器ID
     * @param mainId 流ID
     */
    public void startRecord(String mediaServerId, String mainId, String custompath) {
        try {
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.add("AccessKey", ACCESS_KEY);
            httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

            JSONObject params = new JSONObject();
            params.put("mediaServerId", mediaServerId);
            params.put("mainId", mainId);
            HttpEntity<String> entity = new HttpEntity<>(params.toString(), httpHeaders);
            String url = API_HOST + START_RECORD + "?mediaServerId=" + mediaServerId + "&mainId=" + mainId;
            if (StringUtils.isNotEmpty(custompath)) {
                url = url +  "&customizedPath=" + custompath;
            }
            ResponseEntity<JSONObject> exchange = restTemplate.exchange(url, HttpMethod.GET, entity, JSONObject.class);
            JSONObject body = exchange.getBody();
            Integer code = (Integer) body.get("code");
            if (code != null && code == 0) {
                System.out.println("开始录像调用成功");
            } else {
                throw new Exception(body.get("message").toString());
            }
        } catch (Exception e) {
            System.out.println("开始录像调用失败" + e.toString());
        }
    }

    /**
     * 调用结束录制接口
     * @param mediaServerId 流媒体服务器ID
     * @param mainId 流ID
     */
    public void stopRecord(String mediaServerId, String mainId) {
        try {
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.add("AccessKey", ACCESS_KEY);
            httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

            JSONObject params = new JSONObject();
            params.put("mediaServerId", mediaServerId);
            params.put("mainId", mainId);
            HttpEntity<String> entity = new HttpEntity<>(params.toString(), httpHeaders);
            ResponseEntity<JSONObject> exchange = restTemplate.exchange(API_HOST + STOP_RECORD + "?mediaServerId=" +
                    mediaServerId + "&mainId=" + mainId, HttpMethod.GET, entity, JSONObject.class);
            JSONObject body = exchange.getBody();
            Integer code = (Integer) body.get("code");
            if (code != null && code == 0) {
                System.out.println("结束录像调用成功");
            } else {
                throw new Exception(body.get("message").toString());
            }
        } catch (Exception e) {
            System.out.println("结束录像调用失败" + e.toString());
        }
    }
}
